import { Service } from 'egg';
import { AppConfig } from '../../typings';

/**
 * AppConfig Service
 */
export default class AppConfigService extends Service {
  /**
   * 获取配置
   */
  public async get(): Promise <AppConfig> {
    const { app } = this;

    try {
      // 调用appConsul方法，获取所有配置
      const _conf = await app.m7Consul.getConfig();

      const config: AppConfig = {
        // 基础服务
        // commonDB
        commonDbIp: _conf.service.commonDB.host,
        commonDbPort: _conf.service.commonDB.port,
        commonDbDbName: app.m7Consul.commonDB('dbName/cc'),
        commonDbUserName: app.m7Consul.commonDB('username'),
        commonDbPassword: app.m7Consul.commonDB('password'),
        commonDbPoolSize: parseInt(app.m7Consul.commonDB('poolSize/100'), 10),
        commonDbHaInterval: parseInt(app.m7Consul.commonDB('haInterval/30000'), 10),
        // reportDB
        reportDbIp: _conf.service.reportDB.host,
        reportDbPort: _conf.service.reportDB.port,
        reportDbDbName: app.m7Consul.reportDB('dbName/report'),
        reportDbUserName: app.m7Consul.reportDB('username'),
        reportDbPassword: app.m7Consul.reportDB('password'),
        reportDbPoolSize: parseInt(app.m7Consul.reportDB('poolSize/100'), 10),
        reportDbHaInterval: parseInt(app.m7Consul.reportDB('haInterval/30000'), 10),
        // ccRedis
        ccRedisSentinelHost: _conf.service.ccRedisSentinel.host,
        ccRedisSentinelPort: _conf.service.ccRedisSentinel.port,
        ccRedisSentinelPwd: app.m7Consul.ccRedis('sentinelPassword'),
        // es
        esUrl: `http://${_conf.service.esHttp.host}:${_conf.service.esHttp.port}`,
      };

      return config;
    } catch (e) {
      throw e;
    }
  }
}
